package com.thread;

public class SyncDemo1 {
    static boolean success1 = false;//线程1取款成功标志
    static boolean success2 = false;//线程2取款成功标志

    public static void main(String[] args) {
        Bank bank = new Bank();
        while (true) {
            Thread t1 = new Thread("zs") {
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };

            Thread t2 = new Thread("ls") {
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };

            t1.start();
            t2.start();

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if (success1 && success2) {
                System.out.println("取款成功");
                break;
            } else {
                System.out.println("取款失败");
                bank.saveAccount(20000);//重新存入，再次尝试
                success1 = false;
                success2 = false;
            }
        }
    }
}
